<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <title>数据分析 - 管理后台</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="${pageContext.request.contextPath}/logo/favicon.ico">
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/js/chart.min.js"></script>
    <style>
        .sidebar {
            min-height: calc(100vh - 56px);
        }
        .stat-card {
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .stat-card h5 {
            margin-bottom: 10px;
            color: #666;
            font-size: 14px;
        }
        .stat-card h2 {
            margin: 0;
            font-size: 32px;
            font-weight: bold;
        }
        .chart-container {
            position: relative;
            height: 250px;
            margin-bottom: 20px;
            max-width: 100%;
        }
        .chart-container-large {
            position: relative;
            height: 300px;
            margin-bottom: 20px;
            max-width: 100%;
        }
        .card {
            margin-bottom: 20px;
        }
        .card-header {
            padding: 10px 15px;
        }
        .card-header h5 {
            font-size: 16px;
            margin: 0;
        }
        .card-body {
            padding: 15px;
        }
    </style>
</head>
<body>
    <jsp:include page="common/header.jsp"/>
    
    <div class="container-fluid">
        <div class="row">
            <jsp:include page="common/sidebar.jsp"/>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="pt-3">
                    <h1 class="h2 mb-4">数据分析</h1>
                    
                    <c:if test="${error != null}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            ${error}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    </c:if>
                    
                    <!-- 数据卡片 -->
                    <div class="row mb-4" id="statCards">
                        <div class="col-md-3">
                            <div class="stat-card bg-primary text-white">
                                <h5>用户总数</h5>
                                <h2 id="userCount">-</h2>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-success text-white">
                                <h5>商品总数</h5>
                                <h2 id="productCount">-</h2>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-info text-white">
                                <h5>订单总数</h5>
                                <h2 id="orderCount">-</h2>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-warning text-white">
                                <h5>总销售额</h5>
                                <h2 id="totalSales">¥0.00</h2>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 图表区域 -->
                    <div class="row">
                        <!-- 订单状态分布（饼状图） -->
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-dark text-white">
                                    <h5 class="mb-0">订单状态分布</h5>
                                </div>
                                <div class="card-body">
                                    <div class="chart-container">
                                        <canvas id="orderStatusPieChart"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 用户类型分布（饼状图） -->
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-dark text-white">
                                    <h5 class="mb-0">用户类型分布</h5>
                                </div>
                                <div class="card-body">
                                    <div class="chart-container">
                                        <canvas id="userTypePieChart"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row mt-3">
                        <!-- 商品分类分布（饼状图） -->
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-dark text-white">
                                    <h5 class="mb-0">商品分类分布</h5>
                                </div>
                                <div class="card-body">
                                    <div class="chart-container">
                                        <canvas id="categoryPieChart"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 订单状态统计（柱状图） -->
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-dark text-white">
                                    <h5 class="mb-0">订单状态统计</h5>
                                </div>
                                <div class="card-body">
                                    <div class="chart-container">
                                        <canvas id="orderStatusBarChart"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row mt-3">
                        <!-- 商品分类统计（柱状图） -->
                        <div class="col-md-8 offset-md-2">
                            <div class="card">
                                <div class="card-header bg-dark text-white">
                                    <h5 class="mb-0">商品分类统计</h5>
                                </div>
                                <div class="card-body">
                                    <div class="chart-container-large">
                                        <canvas id="categoryBarChart"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <script src="${pageContext.request.contextPath}/js/bootstrap.bundle.min.js"></script>
    <script>
        // 等待 Chart.js 加载完成后再执行
        window.addEventListener('DOMContentLoaded', function() {
            // 检查 Chart 是否已加载
            if (typeof Chart === 'undefined') {
                console.error('Chart.js 未加载，请检查网络连接或CDN链接');
                alert('图表库加载失败，请刷新页面重试');
                return;
            }
            
            // 加载统计数据
            fetch('${pageContext.request.contextPath}/admin/statistics?action=data')
            .then(response => response.json())
            .then(data => {
                // 更新数据卡片
                document.getElementById('userCount').textContent = data.userCount || 0;
                document.getElementById('productCount').textContent = data.productCount || 0;
                document.getElementById('orderCount').textContent = data.orderCount || 0;
                document.getElementById('totalSales').textContent = '¥' + parseFloat(data.totalSales || 0).toFixed(2);
                
                // 订单状态饼状图
                const orderStatusPieCtx = document.getElementById('orderStatusPieChart').getContext('2d');
                const orderStatusPieData = {
                    labels: Object.keys(data.orderStatusCount || {}),
                    datasets: [{
                        data: Object.values(data.orderStatusCount || {}),
                        backgroundColor: [
                            '#FF6384',
                            '#36A2EB',
                            '#FFCE56',
                            '#4BC0C0',
                            '#9966FF',
                            '#FF9F40'
                        ]
                    }]
                };
                new Chart(orderStatusPieCtx, {
                    type: 'pie',
                    data: orderStatusPieData,
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        plugins: {
                            legend: {
                                position: 'bottom'
                            }
                        }
                    }
                });
                
                // 用户类型饼状图
                const userTypePieCtx = document.getElementById('userTypePieChart').getContext('2d');
                const userTypePieData = {
                    labels: Object.keys(data.userTypeCount || {}),
                    datasets: [{
                        data: Object.values(data.userTypeCount || {}),
                        backgroundColor: [
                            '#36A2EB',
                            '#4BC0C0',
                            '#9966FF'
                        ]
                    }]
                };
                new Chart(userTypePieCtx, {
                    type: 'pie',
                    data: userTypePieData,
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        plugins: {
                            legend: {
                                position: 'bottom'
                            }
                        }
                    }
                });
                
                // 商品分类饼状图
                const categoryPieCtx = document.getElementById('categoryPieChart').getContext('2d');
                const categoryPieData = {
                    labels: Object.keys(data.categoryCount || {}),
                    datasets: [{
                        data: Object.values(data.categoryCount || {}),
                        backgroundColor: [
                            '#FF6384',
                            '#36A2EB',
                            '#FFCE56',
                            '#4BC0C0',
                            '#9966FF',
                            '#FF9F40',
                            '#C9CBCF'
                        ]
                    }]
                };
                new Chart(categoryPieCtx, {
                    type: 'pie',
                    data: categoryPieData,
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        plugins: {
                            legend: {
                                position: 'bottom'
                            }
                        }
                    }
                });
                
                // 订单状态柱状图
                const orderStatusBarCtx = document.getElementById('orderStatusBarChart').getContext('2d');
                const orderStatusBarData = {
                    labels: Object.keys(data.orderStatusCount || {}),
                    datasets: [{
                        label: '订单数量',
                        data: Object.values(data.orderStatusCount || {}),
                        backgroundColor: '#36A2EB'
                    }]
                };
                new Chart(orderStatusBarCtx, {
                    type: 'bar',
                    data: orderStatusBarData,
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        },
                        plugins: {
                            legend: {
                                display: false
                            }
                        }
                    }
                });
                
                // 商品分类柱状图
                const categoryBarCtx = document.getElementById('categoryBarChart').getContext('2d');
                const categoryBarData = {
                    labels: Object.keys(data.categoryCount || {}),
                    datasets: [{
                        label: '商品数量',
                        data: Object.values(data.categoryCount || {}),
                        backgroundColor: '#4BC0C0'
                    }]
                };
                new Chart(categoryBarCtx, {
                    type: 'bar',
                    data: categoryBarData,
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        },
                        plugins: {
                            legend: {
                                display: false
                            }
                        }
                    }
                });
            })
            .catch(error => {
                console.error('加载统计数据失败:', error);
                alert('加载统计数据失败，请刷新页面重试');
            });
        });
    </script>
</body>
</html>

